import store from '@/common/store';
import request from '@/common/request'

const common = {
	toIndex(){
		let userInfo = store.getters.GET_userInfo;
		console.log(userInfo);
		if(userInfo != null){
			//代理，合伙人
			if(userInfo.roleType == 1 || userInfo.roleType == 3){
				uni.reLaunch({
					 url: '/pages/agent/index'
				});
				return;
			}else{
				//商户，店员，其它
				uni.reLaunch({
					 url: '/pages/index/index'
				});
				return;
			}
		}
		uni.reLaunch({
			 url: '/pages/login/login'
		});
	},
	toBack(){
		// #ifdef H5
		history.back();
		// #endif
		
		// #ifndef H5
		uni.navigateBack();
		// #endif
	},
	toPageByScan(qrcodeStr){
		let arr = qrcodeStr.split('/');
		let type = arr[arr.length - 3];
		if(type == 'dev'){
			let deviceSn = arr[arr.length - 1];
			let deviceType = arr[arr.length - 2];
			if(deviceType == 'bxscn'){
				uni.navigateTo({
					url:'/device/bxscn/info?id=' + deviceSn
				})
			}else if(deviceType == 'cdb'){
				uni.navigateTo({
					url:'/device/cdb/info?id=' + deviceSn
				})
			}else if(deviceType == 'cdcz'){
				uni.navigateTo({
					url:'/device/cdcz/info?id=' + deviceSn
				})
			}else if(deviceType == 'pcl'){
				uni.navigateTo({
					url:'/device/pcl/info?id=' + deviceSn
				})
			}
		}
	},
	findLevelType:function(callback){
		var self = this;
		var _key = '_levelTypeList';
		try {
			const data = uni.getStorageSync(_key);
			if (data) {
				callback.call(self,data);
				return;
			}
		} catch (e) {
			// error
		}
		
		request(url.levelTypeList).then(res => {
			uni.setStorage({
				key: _key,
				data: res.levelTypeList
			});
			callback.call(self,res.levelTypeList);
		});
	}
}
module.exports = common
